package cn.kgc.spring.factory;

import com.mysql.jdbc.Driver;
import org.springframework.beans.factory.FactoryBean;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author 课工场
 * @date 2024/2/20
 * @description  获取数据库连接的工厂对象
 *   实现了FactoryBean<Connection>接口类 托管到spring容器后
 *  在获取对象时，返回的不在是该类类型的对象 而是FactoryBean接口中getObject()方法返回的值
 */
public class ConnectionFactoryBean implements FactoryBean<Connection> {

    // 在托管ConnectionFactoryBean之后 获取对象时 返回的是getObject方法的返回值
    @Override
    public Connection getObject() throws Exception {
        System.out.println("===========getObject()=========");
        //  注册驱动
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);

        String username = "root";
        String password = "root";
        String url = "jdbc:mysql:///java2323?useSSL=true";
        Connection connection = DriverManager.getConnection(url, username, password);
        return connection;
    }

    @Override
    public Class<?> getObjectType() {
        return Connection.class;
    }
}
